package com.yst.webplugin.component;

import com.yst.web.utils.ClassUtils;
import com.yst.webplugin.annotation.HttpInfo;
import com.yst.webplugin.fieldhandler.FieldHandler;
import com.yst.webplugin.pathhandler.IPathHandler;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class FieldHandlerAdapter implements HandlerAdapter, Ordered {
    @Override
    public boolean supports(Object handler) {
        return (handler instanceof FieldHandler||ClassUtils.classImplInterface(handler.getClass(), IPathHandler.class));
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof FieldHandler){
            ((FieldHandler)handler).handle(request,response);
        }

        if (ClassUtils.classImplInterface(handler.getClass(), IPathHandler.class)){
            ((IPathHandler)handler).handle(new HttpInfo(request,response));
        }
        return null;
    }

    @Override
    public long getLastModified(HttpServletRequest request, Object handler) {
        return 0;
    }

    private int order=20;
    public void setOrder(int order){
        this.order=order;
    }
    @Override
    public int getOrder() {
        return order;
    }
}
